/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-violationInquiry
*
* @File name : JuHeViolationController.java
*
* @Author : Administrator
*
* @Date : 2017年12月12日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月12日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.violationInquiry.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.violationInquiry.constant.ViolationInquiryConstant;
import com.yonyou.gmmc.service.violationInquiry.model.TmViolationCity;
import com.yonyou.gmmc.service.violationInquiry.service.JuHeViolationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator
 * @date 2017年12月12日
 */
@RestController
@RequestMapping(value = ViolationInquiryConstant.BASE_PATH + "/" + ViolationInquiryConstant.VERSION + "/" + "juhe")
@Api("聚合数据-违章查询API")
public class JuHeViolationController {

	private Logger logger = Logger.getLogger(JuHeViolationController.class);

	@Autowired
	private JuHeViolationService juHeViolationService;

	@RequestMapping(value = "/violationCity", method = RequestMethod.GET)
	@ApiOperation(value = "获取支持城市-聚合数据", notes = "获取支持城市-聚合数据")
	public Response<String> violationCity() {
		logger.info("JuHeViolationController.violationCity==>start...");
		try {
			String result = juHeViolationService.violationCity();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result);
		} catch (Exception e) {
			logger.error("获取城市发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("JuHeViolationController.violationCity==>end...");
		}
	}

	@RequestMapping(value = "/citys", method = RequestMethod.GET)
	@ApiOperation(value = "前端获取城市列表", notes = "前端获取城市列表")
	public Response<List<Map<String, Object>>> checkCitys() {
		logger.info("JuHeViolationController.checkCitys==>start...");
		try {
			List<Map<String, Object>> resultList = juHeViolationService.checkCitys();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultList, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("JuHeViolationController.checkCitys==>end...");
		}
	}

	@RequestMapping(value = "/engineAndVin", method = RequestMethod.GET)
	@ApiOperation(value = "根据城市获取发动机和车架号", notes = "根据城市获取发动机和车架号")
	public Response<List<TmViolationCity>> checkEngineAndVin(@RequestParam("cityCode") String cityCode) {
		logger.info("JuHeViolationController.checkEngineAndVin==>start...");
		try {
			List<TmViolationCity> result = new ArrayList<>();
			result = juHeViolationService.checkEngineAndVin(cityCode);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("JuHeViolationController.checkEngineAndVin==>end...");
		}
	}

	@RequestMapping(value = "/violation", method = RequestMethod.GET)
	@ApiOperation(value = "查询违章记录", notes = "查询违章记录")
	public Response<Map<String, Object>> violation(
			@RequestParam(required = true, defaultValue = "贵BAX383") String registNo,
			@RequestParam(required = false, defaultValue = "") String vin,
			@RequestParam(required = false, defaultValue = "") String engine,
			@RequestParam(required = true, defaultValue = "") String city,
			@RequestParam(value = "userId_", required = false, defaultValue = "") String userId_,
			HttpServletRequest request) {
		logger.info("JuHeViolationController.violation==>start...");
		String userId = "";
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (!CheckUtil.NullOrEmpty(userId_)) {
				userId = userId_;
			} else {
				userId = request.getHeader(CommonConstant.HEAD_USER_ID);
			}
			Map<String, String> map = new HashMap<>();
			map.put("registNo", registNo);
			map.put("vin", vin);
			map.put("engine", engine);
			map.put("city", city);
			map.put("userId", userId);
			result = juHeViolationService.violation(map);
			return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("JuHeViolationController.violation==>end...");
		}
	}

	/**
	 * 进入违章页面前调用：校验车主
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/checkCar", method = RequestMethod.GET)
	@ApiOperation(value = "校验车主", notes = "校验车主")
	@ParamValid
	public Response<Map<String, Object>> checkCar(HttpServletRequest request,
			@RequestParam(value = "userId_", required = false, defaultValue = "") String userId_) {
		logger.info("ViolationInquiryController.checkCar==>start...");
		String userId = "";
		try {
			if (!CheckUtil.NullOrEmpty(userId_)) {
				userId = userId_;
			} else {
				userId = request.getHeader(CommonConstant.HEAD_USER_ID);
			}
			Map<String, Object> map = new HashMap<>();
			map.put("userId", userId);
			Map<String, Object> result = juHeViolationService.checkCar(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.checkCar==>end...");
		}
	}
}
